const { Tool, API, Event, Storage} = global.$util
Page({
  data: {
    shopUserList: [],
    shopId: '',
    isLoading: false,
    isMore: true,
    page: 1,
    tipVal: '', // 默认是无 取值 1 2 3
    currentPage: 1, // 当前的页数
    pageSize: 10, // 每次加载请求的条数 默认10
    params: {},
    isAjax: false 
  },
  onLoad: function (options) {
    this.setData({
        shopId: options.shopId
    })
    this.getShopUserList()
  },
  getShopUserList() {
    if (this.data.isAjax) return
      this.data.isAjax = true
      const params = {
        ...this.data.params,
        pageNum: this.data.currentPage,
        shopId: this.data.shopId,
        pageSize: this.data.pageSize
      }
    API.getShopUserList(params).then((res) => {
      let datas = res.data || {}
            if (datas.total > 0) {
                let isMore = Tool.hasNextPage(this.data.currentPage, this.data.pageSize, datas.total, datas.rows.length)
                this.setData({
                  shopUserList: res.data.rows || [],
                  tipVal: '',
                  isMore: isMore,
                }, ()=> {
                    this.data.loadMoreView = this.selectComponent(".loadMoreView")
                    this.data.loadMoreView.loadMoreComplete()
                })
            } else if (!datas.total) {
                this.setData({
                    tipVal: 2,
                    shopUserList: [],
                    isMore: false
                })
            }
            this.data.isAjax = false
    }).catch((res) => {
      if (this.data.params.page > 1) {
        this.data.currentPage--
      }
      this.data.isAjax = false
    })
  },
  onScroll() { // 向下滑动的时候请求数据
    this.data.loadMoreView.loadMore()
  },
  _loadMoreListener(){
      if (this.data.isAjax) return
      let page = this.data.currentPage
      page += 1
      let {params} = this.data
      params.pageNum = page

      this.data.params = params
      this.data.currentPage = page
      this.getShopUserList(this.data.params)
  },
  toAdd() {
    Tool.navigateTo(`/pages/mine/shop/user/add-user/add-user?shopId=${this.data.shopId}`)
  },
  onClose(event) {
    const { position, instance } = event.detail;
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除吗？',
        }).then(() => {
            API.delClerk({id: event.currentTarget.dataset.id}).then((res) => {
                this.setData({
                  shopUserList: res.data.rows || []
                })
              }).catch((res) => {
                console.error(res)
              })
        });
        break;
    }
  },
})
